//
//  UserInfoPageViewController.m
//  SchoolTimetable
//
//  Created by 梦里都是梦 on 16/1/16.
//  Copyright © 2016年 Yuanlinqian. All rights reserved.
//

#import "UserInfoPageViewController.h"

@interface UserInfoPageViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userNameTF;
@property (weak, nonatomic) IBOutlet UITextField *userPhoneNumTF;
@property (weak, nonatomic) IBOutlet UITextField *userNickNameTF;
@property (weak, nonatomic) IBOutlet UITextField *userRealNameTF;
@property (weak, nonatomic) IBOutlet UISegmentedControl *userSexSC;
@property (weak, nonatomic) IBOutlet UITextField *userAgeTF;
@property (weak, nonatomic) IBOutlet UITextField *userUniversityTF;
@property (weak, nonatomic) IBOutlet UITextField *userGoUniYearTF;
@property (weak, nonatomic) IBOutlet UIButton *editBtn;

@end

@implementation UserInfoPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self refreshUserInfo];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backBarButtonItemPredded:)];
    // Do any additional setup after loading the view.
}

-(void)backBarButtonItemPredded:(UIBarButtonItem *)sender
{
    [AnimationTool setAnimationWith:kCATransitionPush and:kCATransitionFromLeft andTime:0.7 with:self];
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)refreshUserInfo
{
    BmobUser *bUser = [BmobUser getCurrentUser];
    if (bUser) {
        self.userNameTF.text = bUser.username;
        self.userPhoneNumTF.text = bUser.mobilePhoneNumber;
        self.userNickNameTF.text = [bUser objectForKey:@"userNickName"];
        self.userRealNameTF.text = [bUser objectForKey:@"userRealName"];
        self.userAgeTF.text = [bUser objectForKey:@"userAge"];
       NSString * sexStr = [bUser objectForKey:@"userSex"];
        if ([sexStr isEqualToString:@"男"]) {
            self.userSexSC.selectedSegmentIndex = 0;
        }
        else
            self.userSexSC.selectedSegmentIndex = 1;
        self.userUniversityTF.text = [bUser objectForKey:@"userUniversity"];
        self.userGoUniYearTF.text = [bUser objectForKey:@"userGoUniYear"];
        
    }else{
        [SVProgressHUD showErrorWithStatus:@"读取数据失败"];

    }

}
- (IBAction)editBtnPressed:(UIButton *)sender {
    BmobUser *bUser = [BmobUser getCurrentUser];
    if (bUser) {
        [bUser setObject:self.userNickNameTF.text forKey:@"userNickName"];
        [bUser setObject:self.userRealNameTF.text forKey:@"userRealName"];
        [bUser setObject:self.userAgeTF.text forKey:@"userAge"];
        [bUser setObject:self.userUniversityTF.text forKey:@"userUniversity"];
        [bUser setObject:self.userGoUniYearTF.text forKey:@"userGoUniYear"];
        if (self.userSexSC.selectedSegmentIndex == 0) {
            [bUser setObject:@"男" forKey:@"userSex"];
        }
        else
            [bUser setObject:@"女" forKey:@"userSex"];
        [bUser updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
            if (isSuccessful) {
                [SVProgressHUD showSuccessWithStatus:@"保存成功"];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.navigationController popToRootViewControllerAnimated:YES];
                    [[NSNotificationCenter defaultCenter]postNotificationName:USER_REFRESH_USERINFO object:nil];
                });
                
            }
        }];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
